package com.jrx.anytxn.param.controller.product;

import com.google.common.collect.Maps;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.annotation.IgnoreConvertField;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.product.PrBlockCodeRes;
import com.jrx.anytxn.param.dto.product.PrBlockCodeRuleReq;
import com.jrx.anytxn.param.dto.product.PrBlockCodeRuleRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IPrBlockCodeRuleService;
import com.jrx.anytxn.param.service.product.IPrBlockCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

/**
 * 封锁码规则
 * @author hcx
 **/
@Api(tags = "封锁码规则API接口")
@RestController
@RequestMapping(value = "/param/prBlockCodeRule")
public class PrBlockCodeRuleController {
    private static final Logger logger = LoggerFactory.getLogger(PrBlockCodeRuleController.class);
    @Autowired
    private InfoService infoService;
    @Autowired
    private IPrBlockCodeRuleService prBlockCodeRuleService;
    @Autowired
    private IPrBlockCodeService iPrBlockCodeService;

    @ApiOperation(value = "根据租户ID和延滞天数查询封锁码规则表信息", notes = "根据租户ID和延滞天数查询封锁码规则表信息")
    @GetMapping(value = "/{tenantId}/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "id", value = "封锁码规则id", required = true)
    })
    public TxnRespResult<PrBlockCodeRuleRes> getById(@NotNull @PathVariable String tenantId, @PathVariable Long id) {
        TxnRespResult<PrBlockCodeRuleRes> result = new TxnRespResult<>();
        PrBlockCodeRuleRes res;
        try {
            res = prBlockCodeRuleService.findByTenIdAndId(tenantId, id);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID分页查询封锁码规则表信息", notes = "根据租户ID分页查询封锁码规则表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrBlockCodeRuleRes>> getAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {

        TxnRespResult<TxnPage<PrBlockCodeRuleRes>> result = new TxnRespResult<>();
        TxnPage<PrBlockCodeRuleRes> response;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            response = prBlockCodeRuleService.findAllPageByTenantId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "新增封锁码规则表信息", notes = "新增封锁码规则表信息")
    @PostMapping("/")
    public TxnRespResult<PrBlockCodeRuleRes> createPreterminationFeeTable(@RequestBody PrBlockCodeRuleReq prBlockCodeRuleReq) {
        PrBlockCodeRuleRes res;
        TxnRespResult<PrBlockCodeRuleRes> result = new TxnRespResult<>();
        try {
            prBlockCodeRuleReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prBlockCodeRuleReq.setUpdateBy(infoService.getUser().getUserName());
            res = prBlockCodeRuleService.addPrBlockCodeRule(prBlockCodeRuleReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑封锁码规则表信息", notes = "编辑封锁码规则表信息")
    @PutMapping("/")
    public TxnRespResult<PrBlockCodeRuleRes> modifyPreterminationFeeTable(@RequestBody PrBlockCodeRuleReq prBlockCodeRuleReq) {
        TxnRespResult<PrBlockCodeRuleRes> result = new TxnRespResult<>();
        PrBlockCodeRuleRes res;
        try {
            prBlockCodeRuleReq.setUpdateBy(infoService.getUser().getUserName());
            res = prBlockCodeRuleService.modifyPrBlockCodeRule(prBlockCodeRuleReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除封锁码规则表信息", notes = "删除封锁码规则表信息")
    @DeleteMapping(value = "/{tenantId}/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "id", value = "封锁码规则id", required = true)
    })
    public TxnRespResult<Boolean> removeId(@NotNull @PathVariable String tenantId, @PathVariable Long id) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);
            flag = prBlockCodeRuleService.deletePrBlockCodeRule(tenantId, id);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "获得封锁码信息",notes = "获得封锁码信息")
    @GetMapping(value = "/list")
    @IgnoreConvertField
    public TxnRespResult<HashMap<String, List<PrBlockCodeRes>>> getAllOrg() {
        TxnRespResult<HashMap<String, List<PrBlockCodeRes>>> result = new TxnRespResult<>();
        List<PrBlockCodeRes> allBlockCode = iPrBlockCodeService.findAllBlockCodeByTenId(String.valueOf(infoService.getUser().getProjectId()));
        HashMap<String, List<PrBlockCodeRes>> sysCodeMap= Maps.newHashMap();
        sysCodeMap.put("listData",allBlockCode);
        return result.getSuccess(sysCodeMap);
    }
}
